/**
Author: YHUAN416
Date:2020/5/24

a simple threadpool lib.

V1.1 2020/5/28
add free thread num.

V1.0 2020/5/24
First Version.
*/
#ifndef _THREAD_POOL_H_
#define _THREAD_POOL_H_

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAXTHREADNUM    (10)/* MAX WORK THREAD */
#define ADMINTICKS      (2)/* admin routine ticks */

typedef void *ThreadPool_Handler;/* handler of ThreadPool */
typedef void (*TaskFunc)(void *);/* task function */

enum ThreadPoolReturnCode{
    TP_SUCCESS          = 0,
    
    TP_NULL_POINT       = -1,
    TP_MALLOC_FAIL      = -2,
    
    TP_MAX_THREADS      = -3,
    TP_INSERT_FAIL      = -4,

    TP_TASK_INIT_FAIL   = -5,
    TP_TASK_INSERT_FAIL = -6,

    TP_FAIL             = -7
};

extern const char *ThreadPool_Version(void);

extern ThreadPool_Handler ThreadPool_Init(int MaxThreads, int ActiveThreads);
extern int ThreadPool_Destory(ThreadPool_Handler handler);

/* Add a task into ThreadPool, if your task do not need arg, you can just input NULL */
extern int ThreadPool_Add_Task_Tail(ThreadPool_Handler handler, TaskFunc task, void *arg);
extern int ThreadPool_Add_Task_Front(ThreadPool_Handler handler, TaskFunc task, void *arg);

extern int ThreadPool_Insert_Threads(ThreadPool_Handler handler, const int num);
extern int ThreadPool_Remove_Threads(ThreadPool_Handler handler, const int num);

extern int ThreadPool_TaskNum(ThreadPool_Handler handler);
extern int ThreadPool_ShutDown(ThreadPool_Handler handler);
extern int ThreadPool_FreeThreads(ThreadPool_Handler handler);
extern int ThreadPool_TotalThreads(ThreadPool_Handler handler);
extern int ThreadPool_ActiveThreads(ThreadPool_Handler handler);

#endif
